package com.dx.mes.webservice.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.jws.WebService;

import com.dx.mes.common.transcoder.util.JacksonUtil;
import com.dx.mes.webservice.bean.*;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;

import com.dx.mes.common.core.his.HistoryManager;
import com.dx.mes.common.transcoder.util.GUIDGenerator;
import com.dx.mes.common.transcoder.util.MqOpe;
import com.dx.mes.common.transcoder.util.Util;
import com.dx.mes.web.common.service.ISendMessageService;
import com.dx.mes.webservice.OrderWebService;

@WebService(targetNamespace="interface")
@Controller
public class OrderWebServiceImpl implements OrderWebService{
	private Logger logger = Logger.getLogger(OrderWebService.class);
	
	@Resource
	private MqOpe mqOpe;
	@Resource
	private HistoryManager historyManager;
	@Resource
	private ISendMessageService sendMessageServiceImpl;
	
    /*@Override
    public String sayHello(String name) {
        System.out.println("WebService sayHello " + name);
        return "Hello " + name + ", nice to meet you.";
    }
    */
    @Override
	public String orderSendMesage(String inTrx) {
        String evt_no = GUIDGenerator.javaGUID();
    	logger.info("接收到的消息是：" + evt_no + inTrx);
        
    	JSONObject json = JSONObject.fromObject(inTrx);
		String message = sendMessageServiceImpl.sendMesage(json.getString("TRX_ID"), evt_no, inTrx);

		logger.info("处理后的消息是：" + evt_no + message);
		if (!Util.isSpaceCheck(message) && historyManager.isTransactionSuccessed(message)) {
			historyManager.sendHistoryMessageToRabbitMQ(evt_no);
		}
		JSONObject  jsonMessage  = JSONObject.fromObject(message);
		 Message     messageInfo = new Message();
		       if("0000000".equals(jsonMessage.getString("rtn_code"))){
				   messageInfo.setRtn_code("S");
			   }else {
				   messageInfo.setRtn_code("E");
			   }
		            messageInfo.setRtn_mesg(jsonMessage.getString("rtn_mesg"));
          String  messInfo = JacksonUtil.toJSONStr(messageInfo);
//		//TODO 将处理后的结果转换成ESB的结构体
//		List<EsbwsdlOC> iTEM_list = new ArrayList<EsbwsdlOC>();
//		EsbwsdlOC iTEM = new EsbwsdlOC();
//		iTEM.setDATAINFIO(message);
//		iTEM_list.add(iTEM);
//
//		List<EsbwsdlOB> lIST_list= new ArrayList<EsbwsdlOB>();
//		EsbwsdlOB lIST = new EsbwsdlOB();
//		lIST.setITEM(iTEM_list);
//		lIST_list.add(lIST);
//
//		List<EsbwsdlOA> hEAD_list = new ArrayList<EsbwsdlOA>();
//		EsbwsdlOA hEAD = new EsbwsdlOA();
//		hEAD.setBIZTRANSACTIONID("");
//		hEAD.setRESULT("0");
//		hEAD.setERRORCODE("");
//		hEAD.setERRORINFO("");
//		hEAD.setCOMMENTS("");
//		hEAD.setSUCCESSCOUNT("1");
//		hEAD_list.add(hEAD);
//
//		EsbwsdlO dATA = new EsbwsdlO();
//		dATA.setHEAD(hEAD_list);
//		dATA.setLIST(lIST_list);
//		logger.info("回复ESB的消息是：" + evt_no + mqOpe.obj2Xml(dATA, "DATA"));
//		return mqOpe.obj2Xml(dATA, "DATA");
		return  messInfo;
	}
}
